package cst.neu.yuejian_31.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.MasterCommentAdapter;
import cst.neu.yuejian_31.bean.AddMasterCommentBean;
import cst.neu.yuejian_31.bean.MasterCommentBean;
import cst.neu.yuejian_31.iface.AddMasterCommentListener;
import cst.neu.yuejian_31.iface.MasterCommentListner;
import cst.neu.yuejian_31.model.AddMasterCommentModel;
import cst.neu.yuejian_31.model.MasterCommentModel;

public class MasterCommentFragment extends Fragment implements View.OnClickListener, AddMasterCommentListener {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MasterCommentAdapter masterCommentAdapter;
    private List<MasterCommentBean> list;
    private View view=null;
    private Context context;
    private String masterId,text,userId;
    private ImageView userPhoto;
    private TextView userName;
    private EditText masterComment;
    private Button btn_mastercomment;

    public String getUserId()
    {
        SharedPreferences sp;
        sp=context.getSharedPreferences("login", Context.MODE_PRIVATE);
        return  sp.getString("user_id","");
    }

    MasterCommentListner mastercommentListner=new MasterCommentListner() {
        @Override
        public void onRespose(List<MasterCommentBean> matsercommentBeen) {
            list=matsercommentBeen;
            masterCommentAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       view= inflater.inflate(R.layout.fragment_mastercomment, container, false);
        initDate();
        btn_mastercomment=(Button)view.findViewById(R.id.button_mastercomment);
        masterComment=(EditText)view.findViewById(R.id.editText_mastercomment);
        btn_mastercomment.setOnClickListener(this);

       return view;



}

    private void initDate() {
        context=getActivity();
        Intent intent=getActivity().getIntent();
        masterId=intent.getStringExtra("master_id");

        userId=getUserId();
        Toast.makeText(getActivity(),masterId, Toast.LENGTH_SHORT).show();


        MasterCommentModel commentModel=new MasterCommentModel();
        commentModel.getmasterCommentList(masterId,mastercommentListner);


        recyclerView= (RecyclerView)view. findViewById(R.id.recyclerView_mastercomment);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        masterCommentAdapter=new MasterCommentAdapter(context);
        masterCommentAdapter.setList(list);
        recyclerView.setAdapter(masterCommentAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));

    }

    @Override
    public void onSuccess(AddMasterCommentBean addMasterCommentBean) {
        Toast.makeText(context, "评论成功", Toast.LENGTH_SHORT).show();
        MasterCommentModel commentModel=new MasterCommentModel();
        commentModel.getmasterCommentList(masterId,mastercommentListner);

    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_mastercomment:
                AddMasterCommentModel am=new AddMasterCommentModel();
                am.addmasterComment(masterId,userId,masterComment.getText().toString(),this);
                break;
        }

    }
}
